import pytest

from api.county_charge import CountyCharge
from api.school_charge import SchoolCharge
from common.get_log import log
from common.mysql import Mysql2

schoolcharge = SchoolCharge()
countycharge = CountyCharge()
mysql2 = Mysql2()


@pytest.fixture(scope="module", autouse=True)
def schoolchargetest(citytoken):
    result = mysql2.select('select * from user where name="米林县"')
    if result == None:
        log.info("执行学校管理的前置条件:创建县级")
        cityid = mysql2.select('select id from user where name="林芝市"')
        countycharge.add_county(citytoken, "testmilin", "米林县", cityid, "123456", "COUNTY")
        log.info("县级创建成功")
    else:
        log.info("学校管理前置条件已存在,县级无需创建")
    log.info("开始执行学校管理相关接口测试用例")
    yield
    mysql2.delete('delete from user where name="米林县"')
    log.info("学校管理相关接口测试完毕")


@pytest.fixture(scope="class", autouse=True)
def countyid():
    """
    此方法为获取学校管理公共参数学校上一级ID(县级id):parentID
    :return:
    """
    countyid = mysql2.select('select id from user where name="米林县";')
    log.info(f"获取整个学校管理接口前置条件学校上级parentID:{countyid}")
    return countyid

@pytest.fixture(scope="class")
def addschool():
    """
    此为添加学校前后置条件
    :return:
    """
    log.info("开始执行添加学校接口测试用例")
    yield
    mysql2.delete('delete from user where name in ("米林一中","米林二中","米林三中","米林四中")')
    log.info("添加学校数据清洗完成")
    log.info("添加学校测试用例执行完毕")


@pytest.fixture()
def deleteschool():
    """
    此为删除学校前后置条件
    :return:
    """
    log.info("开始执行删除学校接口测试用例")
    yield
    log.info("删除学校测试用例执行完毕")


@pytest.fixture()
def schoolid(citytoken, countyid):
    """
    此为删除学校前置条件:创建学校获schoolid
    :return:
    """
    log.info("执行删除学校的前置条件:创建学校")
    resp = schoolcharge.add_school(citytoken, "test111", "米林四中", countyid, "123456", "SCHOOL")
    id = resp.json()['data']['id']  # 获取添加成功的学校的id
    log.info("成功获取学校id")
    return id
